<?php
namespace App\Servers\Backend;

use App\Repositories\Backend\DoctorRepository;

class DoctorServer extends CommonServer
{

    public function __construct(
        DoctorRepository $doctorRepository
    ) {
        $this->doctorRepository = $doctorRepository;
    }

    /**
     * 列表
     * @param  Array $input [search]
     * @return Array
     */
    public function index($input)
    {
        $result['lists']   = $this->doctorRepository->getLists($input);
        return ['获取成功', $result];
    }

    /**
     * 新增
     * @param  Array $input [subject_id, doctor_name, face_image, tags, easy_adepts, detail_adepts, consult_number, registration_number, order]
     * @return Array
     */
    public function create($input)
    {
        $subject_id            = isset($input['subject_id']) ? intval($input['subject_id']) : '';
        $doctor_name           = isset($input['doctor_name']) ? strval($input['doctor_name']) : '';
        $face_image          = isset($input['face_image']) ? strval($input['face_image']) : '';
        $tags                  = isset($input['tags']) ? strval($input['tags']) : '';
        $easy_adepts           = isset($input['easy_adepts']) ? strval($input['easy_adepts']) : '';
        $detail_adepts         = isset($input['detail_adepts']) ? strval($input['detail_adepts']) : 0;
        $consult_number        = isset($input['consult_number']) ? intval($input['consult_number']) : 0;
        $registration_number   = isset($input['registration_number']) ? intval($input['registration_number']) : 0;
        $order                 = isset($input['order']) ? intval($input['order']) : 0;


        if (!$subject_id || !$doctor_name || !$face_image || !$tags || !$easy_adepts || !$detail_adepts || !$consult_number || !$registration_number || !$order) {
            return ['code' => ['x00004', 'system']];
        }

        $result['list'] = $this->doctorRepository->create($subject_id, $doctor_name, $face_image, $tags,$easy_adepts, $detail_adepts, $consult_number, $registration_number, $order);

        if (!$result['list']) {
            return ['code' => ['x00001', 'system']];
        }

        return ['新增成功', $result];
    }

    /**
     * 编辑
     * @param  Array $input [subject_id, doctor_name, face_image, tags, easy_adepts, detail_adepts, consult_number, registration_number, order]
     * @param  Int $id
     * @return Array
     */
    public function update($id, $input)
    {
        $subject_id            = isset($input['subject_id']) ? intval($input['subject_id']) : '';
        $doctor_name           = isset($input['doctor_name']) ? strval($input['doctor_name']) : '';
        $face_image          = isset($input['face_image']) ? strval($input['face_image']) : '';
        $tags                  = isset($input['tags']) ? strval($input['tags']) : '';
        $easy_adepts           = isset($input['easy_adepts']) ? strval($input['easy_adepts']) : '';
        $detail_adepts         = isset($input['detail_adepts']) ? strval($input['detail_adepts']) : 0;
        $consult_number        = isset($input['consult_number']) ? intval($input['consult_number']) : 0;
        $registration_number   = isset($input['registration_number']) ? intval($input['registration_number']) : 0;
        $order                 = isset($input['order']) ? intval($input['order']) : 0;

        if (!$id || !$subject_id || !$doctor_name || !$face_image || !$tags || !$easy_adepts || !$detail_adepts || !$consult_number || !$registration_number || !$order ) {
            return ['code' => ['x00004', 'system']];
        }

        $result = $this->doctorRepository->update($id, $doctor_name, $face_image, $tags, $easy_adepts, $detail_adepts, $consult_number, $registration_number, $order);
        if (!$result) {
            return ['code' => ['x00001', 'system']];
        }
        return ['更新成功', $result];
    }

    /**
     * 删除
     * @param  Int $id
     * @return Array
     */
    public function destroy($id)
    {
        $result = $this->doctorRepository->destroy($id);

        if (!$result) {
            return ['code' => ['x00002', 'system']];
        }

        return ['删除成功', $result];
    }
}
